<?php

namespace App\Controllers\Goods;

use App\Libs\Goods\Goods;
use App\Models\Order\OrderComment;
use App\Models\Order\OrderCommentImg;
use App\Models\Order\OrderCommentPra;

/**
 *    获取商品
 */
class GetGoodsComment extends \App\Libs\Base\BaseAction
{
    protected function beforeRun()
    {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }

    public function execute()
    {
        if (!$this->request->has('gid')) {
            return \App\Libs\Views\Json::view(1, '未指定商品id', []);
        }
        $gid = $this->request->get('gid', 'int', 0);
        $page = $this->request->get('page', 'int', 1);
        $num = $this->request->get('num', 'int', 5);
		
		//获取商品评论
        $offset = ($page - 1) * $num;
        $goodsComment = OrderComment::findAll([
            'conditions' => 'gid=?1',
            'bind' => [1 => $gid],
            'order' => 'ctime desc',
            'offset' => $offset,
            'limit' => $num
        ]);
        if (!$goodsComment->valid()) {
            return \App\Libs\Views\Json::view(1, '商品暂时没有评论', []);
        }
		
		//商品评论处理 同时获取所有的评论id
		$goodsCommentId = [];
        foreach ($goodsComment as $eachComment) {
            $eachCommentArray = $eachComment->toArray();
            $imgModel = $eachComment->commentImg;
            if (!empty($imgModel)) {
                $eachCommentArray['img_url'] = json_decode($imgModel->img_url);
            } else {
                $eachCommentArray['img_url'] = [];
            }
            $userData = $eachComment->user;
            //todo::如果没有就是默认的头像；或者用户注册成功后自动分配默认头像，此步省略;
            if (!empty($userData)) {
                $eachCommentArray['avatar'] = $userData->avatar;
                $eachCommentArray['user_name'] = $userData->username;
            } else {
                $eachCommentArray['avatar'] = '默认头像地址';
                $eachCommentArray['user_name'] = '默认用户名';
            }
			$eachCommentArray['iszan'] = '/static/product/img/praise_off.png';
            $finalData[] = $eachCommentArray;
			$goodsCommentId[] = $eachCommentArray['id'];
        }

		//判断是否登录 如果登录 则展示当前登录用户所点赞的值
		if( isset( $this->session->user ) ){
			//用户登录了
			$finalData = self::commentUser( $this->session->user->id , $finalData , $goodsCommentId );
		}
		

        return \App\Libs\Views\Json::view(0, '成功', $finalData);

    }
	
	/**
	*	展示当前登录用户所点赞的值
	*/
	public static function commentUser( $uid , $finalData , $goodsCommentId ){
		$orderCommentAll = OrderCommentPra::findAll( array( 
			' cid IN ({commentId:array}) and uid = :uid: and show = 1',
			'bind' => array( 'commentId' => $goodsCommentId , 'uid'=>$uid)
		) )->toArray();
		if( empty( $orderCommentAll ) ){
			return $finalData;//不存在点赞，直接返回之前的数据
		}
		foreach( $orderCommentAll as $k=>$v ){
			$orderCommentInfo[$v['cid']] = $v['cid'];
		}
		
		//有点赞
		foreach( $finalData as $k=>$v ){
			if( !empty( $orderCommentInfo[$v['id']] ) ){
				$finalData[$k]['iszan'] = '/static/product/img/praise_on.png';
			}
		}
		return $finalData;
	}
}
